package dragon.modules.system.dic.action;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.json.simple.JSONObject;

import dragon.core.db.DBUtil;
import dragon.core.exception.ExceptionHandler;
import dragon.core.system.po.SysDic;
import dragon.core.util.GlobalParameter;
import dragon.core.util.Handler;
import dragon.core.util.StringUtils;
import dragon.modules.system.dic.dao.DicDaoImpl;

public class DicHandler extends Handler {
	private DicDaoImpl dd = new DicDaoImpl();

	public void request() throws ExceptionHandler {
		String contextPath = this.getServletRequest().getContextPath();
		int home = getParameter("home") == null ? 0 : Integer.parseInt(getParameter("home").trim());
		int id = getParameter("id") == null ? 0 : Integer.parseInt(getParameter("id").trim());
		StringBuffer sb = new StringBuffer();
		List<SysDic> list = null;

		if (id == 0) {
			list = dd.getDics(home, true);
		} else {
			list = dd.getDics(id, true);
		}
		for (SysDic sd : list) {
			if (id != sd.getId()) {
				String tem = "{ id:" + sd.getId() + ", pId:" + sd.getPid() + ", name:\"" + sd.getName() + "\", isParent:" + (sd.getIsleaf().equals("0") ? "true, nocheck:true" : false) + ", url:'"
						+ contextPath + "/webmodules/system/dic/dicList.jsp?pid=" + sd.getId() + "', target:'diclist', open:false },";
				sb.append(tem);
			}
		}
		if (sb.length() > 1) {
			sb.deleteCharAt(sb.length() - 1);
		}
		String treedata = "";
		if (id == 0) {
			if (home != 0) {
				SysDic sdc = dd.getDic(DBUtil.getDatabase(), home, true);
				treedata = "[ " + "{ id:" + sdc.getId() + ", pid:0, name:'" + sdc.getName() + "', open:true, url:'" + contextPath + "/webmodules/system/dic/dicList.jsp?pid=" + sdc.getId()
						+ "', target:'diclist', isParent:true, nocheck:true, icon:'plugins/ztree/zTreeStyle/img/diy/1_close.png'}," + sb.toString() + "]";
			} else {
				treedata = "[ " + "{ id:0, pid:0, name:'Home', open:true, url:'" + contextPath + "/webmodules/system/dic/dicList.jsp?pid=0', target:'diclist', isParent:true, nocheck:true, icon:'"
						+ contextPath + "/plugins/ztree/zTreeStyle/img/diy/1_close.png'}," + sb.toString() + "]";
			}
		} else {
			treedata = "[" + sb.toString() + "]";
		}
		output(treedata);
	}

	/**
	 * 节点移动
	 * 
	 * @throws ExceptionHandler
	 */
	public void changeParent() throws ExceptionHandler {
		String id = getParameter("id");// 待移动的部门ID
		String target = getParameter("target");// 目标父节点ID
		if ("null".equalsIgnoreCase(target)) {
			target = "0";
		}
		if (StringUtils.isInteger(id) && StringUtils.isInteger(target)) {
			dd.changeParent(Integer.parseInt(id), Integer.parseInt(target));
		}
		output(successJSON);
	}

	/**
	 * 改变字典状态
	 * 
	 * @throws ExceptionHandler
	 */
	public void changeState() throws ExceptionHandler {
		String ids = getParameter("ids").trim();
		String state = getParameter("state").trim();
		for (String id : ids.split(",")) {
			if (!StringUtils.isInteger(id) || !StringUtils.isInteger(state)) {
				throw new ExceptionHandler("调用参数无效！");
			}
			dd.changeDicState(Integer.parseInt(id), Integer.parseInt(state));
		}
		output(successJSON);
	}

	/**
	 * 强制为父节点
	 * 
	 * @throws ExceptionHandler
	 */
	public void changeIsLeaf() throws ExceptionHandler {
		String ids = getParameter("ids").trim();
		for (String id : ids.split(",")) {
			if (!StringUtils.isInteger(id)) {
				throw new ExceptionHandler("调用参数无效！");
			}
			dd.changeIsLeaf(DBUtil.getDatabase(), Integer.parseInt(id), "0");
		}
		output(successJSON);
	}

	/**
	 * 用于前台页面中通过combotree的方式调用特定的数据字典 获取特定数据字典时，需要传入根节点，参数名：id
	 * 
	 * @throws ExceptionHandler
	 */
	@SuppressWarnings("unchecked")
	public void combotree() throws ExceptionHandler {
		String pid = getParameter("id");
		String root = getParameter("root");
		if (StringUtils.isEmpty(pid)) {
			pid = root;
		}
		pid = StringUtils.clear(pid, String.valueOf(GlobalParameter.DicRoot));
		DicDaoImpl dicDao = new DicDaoImpl();
		List<SysDic> dics = dicDao.getDics(Integer.parseInt(pid), false);
		pid = null;

		List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
		for (SysDic dic : dics) {
			Map<String, Object> map = new HashMap<String, Object>();
			map.put("id", dic.getId());
			map.put("text", dic.getName());
			map.put("state", dic.getIsleaf().equals("0") ? "closed" : "open");
			Map<String, Object> attributes = new HashMap<String, Object>();
			attributes.put("idpath", dic.getIdpath());
			attributes.put("isleaf", dic.getIsleaf());
			map.put("attributes", attributes);
			list.add(map);
			map = null;
		}
		dics = null;
		dicDao = null;
		JSONObject jsonObject = new JSONObject();
		jsonObject.put("", list);
		String result = jsonObject.toJSONString();
		result = result.substring(4, result.length() - 1);
		output(result);
		jsonObject = null;
		list = null;
		result = null;
	}

}
